package com.eian.boot.mybatis.plugin;

import com.mybatisflex.core.audit.AuditMessage;
import com.mybatisflex.core.audit.MessageCollector;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CustomMessageCollector implements MessageCollector {

    private final SqlDebugPrinter printer = (sql, tookTimeMillis) -> {
        if (tookTimeMillis != null) {
            log.info("exec sql cost: {}ms >>> {}", tookTimeMillis, sql);
        } else {
            log.info("exec sql >>> {}", sql);
        }
    };

    public CustomMessageCollector() {
    }

    @Override
    public void collect(AuditMessage message) {
        printer.print(message.getFullSql(), message.getElapsedTime());
    }

    public interface SqlDebugPrinter {
        void print(String sql, Long tookTimeMillis);
    }
}
